# coding: utf-8
import json
from django.shortcuts import render
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from article.models import Article, Tag, Classification

from django.http import Http404
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.contrib.syndication.views import Feed
from django.core import serializers

# Create your views here.

# 主页对应的视图函数，包括分页、分类、分标签以及归档等过程
def home(request):
	isHome = True
	articles = Article.objects.all()

	# 每个页面最多显示6篇文章
	paginator = Paginator(articles, 6)
	page_num = request.GET.get('page')

	try:
		articles = paginator.page(page_num)
	except PageNotAnInteger:
		articles = paginator.page(1)
	except EmptyPage:
		articles = paginator.page(paginator.num_pages)

	# 分类，以及对应的文章数目，下同
	classification = Classification.class_list.get_Class_List()
	# 标签，以及对应的文章数目，下同
	tagCloud = Tag.tag_list.get_Tag_List()
	# 按月归档，以及对应的文章数目，下同
	monthArchiveList = Article.article_list.get_Articles_onMonth()

	# locals返回的字典对所有局部变量的名称和值进行映射
	return render_to_response('blog/index.html', locals(), context_instance = RequestContext(request))

# detail函数实现对文章内容的访问和显示
def detail(request, year, month, day, id):
	try:
		article = Article.objects.get(id = str(id))
	except Article.DoesNotExist:
		raise Http404

	classification = Classification.class_list.get_Class_List()
	tagCloud = Tag.tag_list.get_Tag_List()
	monthArchiveList = Article.article_list.get_Articles_onMonth()

	return render_to_response('blog/content.html', locals(), context_instance = RequestContext(request))

# 该函数完成了按月归档的过程
def archiveMonth(request, year, month):
	isArchMonth = True
	articles = Article.objects.filter(publish_time__year=year).filter(publish_time__month=month)
	paginator = Paginator(articles, 6)
	page_num = request.GET.get('page')
	try:
		articles = paginator.page(page_num)
	except PageNotAnInteger:
		articles = paginator.page(1)
	except EmptyPage:
		articles = paginator.page(paginator.num_pages)

	classification = Classification.class_list.get_Class_List()
	tagCloud = Tag.tag_list.get_Tag_List()
	monthArchiveList = Article.article_list.get_Articles_onMonth()

	return render_to_response('blog/index.html', locals(), context_instance = RequestContext(request))

# 本函数解决了按分类浏览文章的问题
def classDetail(request, cls):
	isClass = True
	tempClass = Classification.objects.get(name = cls)
	articles = tempClass.article_set.all()

	paginator = Paginator(articles, 6)
	page_num = request.GET.get('page')

	try:
		 articles = paginator.page(page_num)
	except PageNotAnInteger:
		articles = paginator.page(1)
	except EmptyPage:
		 articles = paginator.page(paginator.num_pages)

	classification = Classification.class_list.get_Class_List()    
	tagCloud = Tag.tag_list.get_Tag_List()
	monthArchiveList = Article.article_list.get_Articles_onMonth()
		
	return render_to_response('blog/index.html', locals(), context_instance = RequestContext(request))

# 本函数解决了按标签浏览文章信息的问题
def tagDetail(request, tag):
	isTag = True
	tempTag = Tag.objects.get(name = tag)
	articles = tempTag.article_set.all()

	paginator = Paginator(articles, 6)
	page_num = request.GET.get('page')
	try:
		articles = paginator.page(page_num)
	except PageNotAnInteger:
		articles = paginator.page(1)
	except EmptyPage:
		articles = paginator.page(paginator.num_pages)

	classification = Classification.class_list.get_Class_List()    
	tagCloud = Tag.tag_list.get_Tag_List()
	monthArchiveList = Article.article_list.get_Articles_onMonth()
		
	return render_to_response('blog/index.html', locals(), context_instance = RequestContext(request))

# 个人简历
def cv(request):
	classification = Classification.class_list.get_Class_List()    
	tagCloud = Tag.tag_list.get_Tag_List()
	monthArchiveList = Article.article_list.get_Articles_onMonth()
		
	return render_to_response('blog/cv.html', locals(), context_instance = RequestContext(request))

def archive(request):
	archive = Article.article_list.get_Articles_onArchive()

	classification = Classification.class_list.get_Class_List()    
	tagCloud = Tag.tag_list.get_Tag_List()
	monthArchiveList = Article.article_list.get_Articles_onMonth()
		
	return render_to_response('blog/archive.html', locals(), context_instance = RequestContext(request))

# 实现对文章标题的搜索
def blogSearch(request):
	isSearch = True

	classification = Classification.class_list.get_Class_List()    
	tagCloud = Tag.tag_list.get_Tag_List()
	monthArchiveList = Article.article_list.get_Articles_onMonth()

	error = False
	if 's' in request.GET:
		searchContent = request.GET['s']
		if not searchContent:
			return render(request, 'blog/index.html')
		else:
			articles = Article.objects.filter(title__icontains = searchContent)
			if len(articles) == 0 :
				error = True

	return render_to_response('blog/index.html', locals(), context_instance = RequestContext(request))

def message(request):
	classification = Classification.class_list.get_Class_List()  
	tagCloud = Tag.tag_list.get_Tag_List()
	monthArchiveList = Article.article_list.get_Articles_onMonth()

	return render_to_response('blog/message.html', locals(), context_instance=RequestContext(request))


